<template>
  <!--  <div class="login-container">-->
  <el-row type="flex" class="row-bg" justify="center">
    <el-col :xl="6" :lg="7">
      <h1>
        欢迎使用资源管理中心
      </h1>
      <el-image :src="require('@/assets/login-l.png')" style="width: 70%; height: 70%;"></el-image>
      <!--      <p>-->
      <!--        欢迎使用-->
      <!--      </p>-->
    </el-col>
    <el-col :span="1">
      <el-divider direction="vertical"></el-divider>
    </el-col>
    <el-col :xl="6" :lg="7">
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm">
        <el-form-item>
          <el-image :src="require('@/assets/logo.png')" style="width: 55%;"></el-image>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            prefix-icon="el-icon-user-solid"
            style="width: 100%;"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            name="password"
            show-password
            @keyup.enter.native="handleLogin"
            style="width: 100%;"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            style="width: 100%;"
            @click="handleLogin"
          >登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
  export default {
    name: "Login",
    data() {
      // 自定义验证规则
      const validateName = (rule, value, callback) => {
        if (value !== 'superadmin') {
          callback(new Error('请使用指定用户登录'));
        } else {
          callback();
        }
      };
      return {
        loginForm: {
          username: "",
          password: "",
          apptype: 0,
        },
        loginRules: {
          username: [
            {required: true, message: "请输入用户名", trigger: ["blur", "change"]},
            {
              validator: validateName,
              trigger: ['blur']
            }
          ],
          password: [
            {required: true, message: "请输入密码", trigger: ["blur", "change"],},
            {min: 6, max: 64, message: '密码不能少于6位！', trigger: ["blur", "change"],},
          ],
        },
        loading: false,
        redirect: undefined,
        otherQuery: {},
      };
    },
    watch: {
      $route: {
        handler: function (route) {
          const query = route.query;
          if (query) {
            this.redirect = query.redirect;
            this.otherQuery = this.getOtherQuery(query);
          }
        },
        immediate: true,
      },
    },
    methods: {
      handleLogin() {
        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            this.loading = true;
            this.$store.dispatch("user/login", this.loginForm).then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery,
              });
              this.loading = false;
            }).catch(() => {
              this.loading = false;
            });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      getOtherQuery(query) {
        return Object.keys(query).reduce((acc, cur) => {
          if (cur !== "redirect") {
            acc[cur] = query[cur];
          }
          return acc;
        }, {});
      },
    },
  }
</script>

<style scoped>

  .el-row {
    background-color: #f9fbfd;
    height: 100%;
    display: flex;
    align-items: center;
    text-align: center;
    justify-content: center;
  }

  .el-divider {
    height: 200px;
  }

  .imageClass {
    float: left;
    margin-left: 8px;
    border-radius: 4px;
  }
</style>
